package video

import (
	"fmt"
	"gitee.com/mootok/mootok/src/models"
	"gitee.com/mootok/mootok/src/services/collection"
	"gitee.com/mootok/mootok/src/services/like"
	"gitee.com/mootok/mootok/src/services/user"
	"gitee.com/mootok/mootok/src/storage/database"
	"gitee.com/mootok/mootok/src/storage/file"
	"gitee.com/mootok/mootok/src/web/api"
	"strconv"
	"time"
)

var UserService user.Service
var CollectionService collection.Service
var LikeService like.Service

func (s *Service) ToVideoInfoById(videoIds []int, userId *int, videoInfo *[]api.VideoInfo) error {
	videos := make([]models.Video, len(videoIds))
	database.Client.Model(&models.Video{}).Where("id IN ?", videoIds).Find(&videos)
	return s.ToVideoInfo(videos, userId, videoInfo)
}

// ToVideoInfo converts video info from db model to idl model
func (s *Service) ToVideoInfo(videos []models.Video, userId *int, videoInfo *[]api.VideoInfo) error {
	authorIds := make([]int, len(videos))
	authorInfo := make([]api.UserInfo, len(videos))
	videoIds := make([]int, len(videos))
	isCollects := make([]bool, len(videos))
	isLikes := make([]bool, len(videos))
	likedCount := make([]int, len(videos))
	collectedCount := make([]int, len(videos))
	for i, _ := range videos {
		createdAt := strconv.FormatInt(videos[i].CreatedAt.Unix(), 10)

		coverUrl := file.Instance.GetLink(videos[i].CoverName, LinkExpirationTime, nil)
		videoUrl := file.Instance.GetLink(videos[i].FileName, LinkExpirationTime, nil)

		transcodePolicies := s.GetTranscodePolicy()
		sourceInfo := make([]api.VideoSourceInfo, len(*transcodePolicies)+1)

		expiredAt := strconv.FormatUint(uint64(time.Now().Unix()+LinkExpirationTime), 10)
		pm3u8Cmd := fmt.Sprintf("pm3u8/0/deadline/%s", expiredAt)
		for index, policy := range *transcodePolicies {
			sourceInfo[index].Name = policy.Name
			sourceInfo[index].ExpiredAt = expiredAt
			sourceInfo[index].Type = "hls"
			sourceInfo[index].Url = file.Instance.GetLink(
				s.GetTranscodeUrl(videos[i].FileName, policy),
				LinkExpirationTime,
				&pm3u8Cmd,
			)
		}
		sourceInfo[len(sourceInfo)-1] = api.VideoSourceInfo{
			Name:      "Auto",
			ExpiredAt: expiredAt,
			Type:      "hls",
			Url: file.Instance.GetLink(
				videos[i].FileName+".m3u8",
				LinkExpirationTime,
				&pm3u8Cmd,
			),
		}

		(*videoInfo)[i].Id = &videos[i].ID
		(*videoInfo)[i].Description = &videos[i].Description

		(*videoInfo)[i].CreatedAt = &createdAt
		(*videoInfo)[i].VideoUrl = &videoUrl
		(*videoInfo)[i].CoverUrl = &coverUrl
		(*videoInfo)[i].VideoSources = &sourceInfo
		(*videoInfo)[i].Author = &authorInfo[i]
		(*videoInfo)[i].IsCollected = &isCollects[i]
		(*videoInfo)[i].IsLiked = &isLikes[i]
		(*videoInfo)[i].LikedCount = &likedCount[i]
		(*videoInfo)[i].CollectedCount = &collectedCount[i]
		authorIds[i] = videos[i].UserId
		videoIds[i] = videos[i].ID
	}
	if err := UserService.GetUserInfo(authorIds, userId, &authorInfo); err != nil {
		return err
	}

	if err := CollectionService.CountUsers(videoIds, &collectedCount); err != nil {
		return err
	}
	if err := LikeService.CountUsers(videoIds, &likedCount); err != nil {
		return err
	}

	if userId != nil {
		if err := CollectionService.IsCollected(videoIds, *userId, &isCollects); err != nil {
			return err
		}
		if err := LikeService.IsLiked(videoIds, *userId, &isLikes); err != nil {
			return err
		}
	} else {
		// 未登录用户使用默认的 false
	}

	return nil
}
